package com.chuangke.admin.controller;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.chuangke.admin.dto.DataPermissionRequest;
import com.chuangke.admin.entity.SysDataPermission;
import com.chuangke.admin.service.SysDataPermissionService;
import com.chuangke.common.http.HttpResult;

/**
 * 字典控制器
 * @author chuangke
 * @date Oct 29, 2018
 */
@RestController
@RequestMapping("datapermission")
public class SysDataPermissionController {

	@Autowired
	private SysDataPermissionService sysDataPermissionService;
	
	@PreAuthorize("hasAuthority('sys:dict:add') AND hasAuthority('sys:dict:edit')")
	@PostMapping(value="/save")
	public HttpResult save(@RequestBody DataPermissionRequest dpr) {
		sysDataPermissionService.save(dpr) ;
		return HttpResult.ok();
	}
	
	@PreAuthorize("hasAuthority('sys:dict:delete')")
	@PostMapping(value="/delete")
	public HttpResult delete(@RequestBody List<SysDataPermission> sdps) {
		sysDataPermissionService.removeByIds(sdps.stream().map(SysDataPermission::getId).collect(Collectors.toList()));
		return HttpResult.ok();
	}

	
	@PreAuthorize("hasAuthority('login:action')")
	@GetMapping(value = "/find/{type}")
	public HttpResult findByType(@PathVariable("type") String type) {
		List<SysDataPermission> dictList = sysDataPermissionService.find(type) ;
		return HttpResult.ok(dictList);
	}
	
	@PreAuthorize("hasAuthority('sys:dict:view')")
	@PostMapping(value="/findbymaster")
	public HttpResult findByMaster(@RequestBody DataPermissionRequest dpr) {
		List<SysDataPermission> list = sysDataPermissionService.find(dpr) ;
		return HttpResult.ok(list);
	}
	
}
